package TeamService;
import static TeamService.Data.*;

import Teamdomain.*;

/**
 * @program: Demo2
 * @description:
 * @author: Yan Guang
 * @create: 2020-04-23 11:17
 */
public class NameListService {
    private Employee[] employees;

    public NameListService() {
//        根据项目提供的Data类构建相应大小的employees数组
        employees = new Employee[EMPLOYEES.length];
        for (int i = 0; i < employees.length; i++) {
            int type = Integer.parseInt(EMPLOYEES[i][0]);
            int id = Integer.parseInt(EMPLOYEES[i][1]);
            String name = EMPLOYEES[i][2];
            int age = Integer.parseInt(EMPLOYEES[i][3]);
            double salary = Double.parseDouble(EMPLOYEES[i][4]);
            Equipment equipment;
            double bouns;
            int stock;
            switch (type) {
                case EMPLOYEE:
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case DESIGNER:
                    bouns = Double.parseDouble(EMPLOYEES[i][5]);
                    equipment = createEquipment(i);
                    employees[i] = new Designer(id, name, age, salary, equipment, bouns);
                    break;
                case ARCHITECT:
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    bouns = Double.parseDouble(EMPLOYEES[i][5]);
                    equipment = createEquipment(i);
                    employees[i] = new Architect(id, name, age, salary, equipment, bouns, stock);
                    break;
            }
        }
//        再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，以及相关联的Equipment子类的对象
//                将对象存于数组中
//        Data类位于com.atguigu.team.service包中

    }

    private Equipment createEquipment(int index) {
        int key = Integer.parseInt(EQUIPMENTS[index][0]);
        switch (key) {
            case PC:
                return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
            case NOTEBOOK:
                return new NoteBook(EQUIPMENTS[index][1], Double.parseDouble(EQUIPMENTS[index][2]));
            case PRINTER:
                return new Printer(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
        }
        return null;
    }

    public Employee[] getAllEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException {
        for (int i = 0; i < employees.length; i++) {
            if (!(employees[i].getId() == id)) {
                throw new TeamException("错误");
            }
            return employees[i];
        }
        return null;
    }
}
